package com.baidu.navigation

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.commit


class HomeFragment : Fragment() {
    override fun onAttach(context: Context) {
        super.onAttach(context)
        Log.v("pxd", "onAttach-fragment")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.v("pxd", "onCreate-fragment")
    }

    /**
     * 配置Fragment显示的内容
     */
    @SuppressLint("ClickableViewAccessibility")
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_home, container, false)
        val pupleView = view.findViewById<View>(R.id.purple)
        view.setOnTouchListener { v, event ->
            if (event?.action == MotionEvent.ACTION_DOWN){
                //切换Fragment
                parentFragmentManager.commit {
                    //add(R.id.fragmentContainerView,HomeFragment())
                    setReorderingAllowed(true)
                    replace(R.id.fragmentContainerView,DetailFragment())
                    addSharedElement(pupleView,"orange")
                    //fragment切换过去 还想能够返回回来
                    addToBackStack(null)
                }
            }
            true
        }
        return view
    }

    /**
     * 如何获得fragment对应的Activity
     * requireActivity()
     *
     * 获取fragment宿主对应的FragmentManager
     * getParentFragmentManager()
     *
     * 获取当前这个fragment对应的FragmentManager
     * getChildFragmentManager()
     */


    override fun onResume() {
        super.onResume()
        Log.v("pxd", "onResume-fragment")
    }

    override fun onStart() {
        super.onStart()
        Log.v("pxd", "onStart-fragment")
    }

    override fun onPause() {
        super.onPause()

        Log.v("pxd", "onPause-fragment")
    }

    override fun onStop() {
        super.onStop()

        Log.v("pxd", "onStop-fragment")
    }

    override fun onDestroy() {
        super.onDestroy()

        Log.v("pxd", "onDestroy-fragment")
    }

    override fun onDetach() {
        super.onDetach()

        Log.v("pxd", "onDetach-fragment")
    }
}